package test;

import com.jayway.jsonpath.internal.path.PathCompiler;
import com.nkzy.Pojo.user;
import com.nkzy.controller.userController;
import org.junit.jupiter.api.Test;

public class usertest {
    private PathCompiler Assert;

    @Test
    public void insertUser() {
        user user = new user();
        user.setUsername("test");
        user.setPassword("test");
        user.setEmail("test@test.com");
        userController userController = new userController();

        try {
            if (user != null) {
                com.nkzy.controller.userController.Response<String> response = userController.insertUser(user);
            } else {
                // 处理 user 为 null 的情况，例如记录日志或抛出异常
                throw new IllegalArgumentException("User object cannot be null");
            }
        } catch (Exception e) {
            // 处理异常，例如记录日志或返回错误响应
            e.printStackTrace();
            // 或者返回一个包含错误信息的 Response 对象
            com.nkzy.controller.userController.Response<String> errorResponse = new userController.Response<>();
            errorResponse.setMessage("Failed to insert user: " + e.getMessage());
            errorResponse.setSuccess(false);
            // 根据实际情况处理 errorResponse
        }


    }
    @Test
    public void selectUserById() {
        userController userController = new userController();
        try {
            user user = null;
            if (user != null) {
                com.nkzy.controller.userController.Response<String> response = userController.insertUser(user);
            } else {
                // 处理 user 为 null 的情况，例如记录日志或抛出异常
                throw new IllegalArgumentException("User object cannot be null");
            }
        } catch (Exception e) {
            // 处理异常，例如记录日志或返回错误响应
            e.printStackTrace();
            // 或者返回一个包含错误信息的 Response 对象
            com.nkzy.controller.userController.Response<String> errorResponse = new userController.Response<>();
            errorResponse.setMessage("Failed to insert user: " + e.getMessage());
            errorResponse.setSuccess(false);
            // 根据实际情况处理 errorResponse
        }

    }
    @Test
    public void updateUser() {
        user user = new user();
        user.setId(1);
        user.setUsername("test");
        user.setPassword("test");
        user.setEmail("test@test.com");
        userController userController = new userController();
        var response = 0;
        try {
            if (user != null) {
                response = userController.updateUser(user).getCode();
            } else {
                // 处理 user 为 null 的情况，例如记录日志或抛出异常
                throw new IllegalArgumentException("User object cannot be null");
            }
        } catch (Exception e) {
            // 处理异常，例如记录日志或返回错误响应
            e.printStackTrace();
        }

        if (response == 200) {
            System.out.println("更新成功");
        } else {
            System.out.println("更新失败");
        }

    }
    @Test
    public void deleteUserById() {
        userController userController = new userController();
        try {
            com.nkzy.controller.userController.Response<String> response = userController.deleteUserById(1);
            if (response != null && response.isSuccess()) {
                // 删除操作成功，可以根据需要处理响应信息
                System.out.println("用户删除成功: " + response.getData());
            } else {
                // 删除操作失败，记录日志或进行其他处理
                System.err.println("用户删除失败: " + (response != null ? response.getData() : "未知错误"));
            }
        } catch (Exception e) {
            // 捕获并处理异常，记录日志
            System.err.println("删除用户时发生异常: " + e.getMessage());
            e.printStackTrace();
        }

    }

    @Test
    public void selectUserByUsername() {
        userController userController = new userController();
        try {
            com.nkzy.controller.userController.Response<user> response = userController.selectUserByUsername("test");
            System.out.println(String.format("Code: %d", response.getCode()));
            System.out.println(String.format("Message: %s", response.getMessage()));
            System.out.println(String.format("Data: %s", response.getData()));

            if (response.getCode() == 200) {
                System.out.println("查询成功");
            } else {
                System.out.println("查询失败");
            }
        } catch (Exception e) {
            System.err.println("查询过程中发生异常: " + e.getMessage());
            e.printStackTrace();
        }
    }

}
